package yxy.game.pm2.module.jadeseal;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.medaldata;
import yxy.cherry.data.bean.medalgrade;
import yxy.cherry.data.bean.medalrefine;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//玉玺
public class JadeSealModule extends PlayerModule {
	// bean===========================================================================

	private HashMap<Integer, JadeSeal> jss = new HashMap<Integer, JadeSeal>();

	public HashMap<Integer, JadeSeal> getJss() {
		return jss;
	}
	public void setJss(HashMap<Integer, JadeSeal> jss) {
		this.jss = jss;
	}

	// func===========================================================================

	private static JadeSealModule create(String playerCode) {
		JadeSealModule module = new JadeSealModule();
		module.setPlayerCode(playerCode);

		Map<String, medaldata> datamap = DataCenter.getAllData(medaldata.class);
		for (medaldata data : datamap.values()) {
			JadeSeal js = new JadeSeal();
			js.setCamp(data.getType());
			js.setOpen(false);
			js.setExp(0);
			js.setLevel(0);
			js.setStagelevel(0);
			js.setStagestage(0);

			module.getJss().put(data.getType(), js);
		}

		module.set();
		return module;
	}

	// redis==========================================================================
	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:JADESEAL", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public JadeSealModule get(String playerCode) {
		try {
			JadeSealModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), JadeSealModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}
			if (module == null) {
				module = create(playerCode);
			}
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private void check() {
		Map<Integer, JadeSeal> jsmap = getJss();
		
		Map<String, medalgrade> mgmap = DataCenter.getAllData(medalgrade.class);
		Map<String, medalrefine> mrmap = DataCenter.getAllData(medalrefine.class);
		
		boolean level = false;
		boolean stage = false;
		
		for(JadeSeal js : jsmap.values()) {
			if(!js.isOpen()) {
				continue;
			}
			
			//检查升级
			if(!level) {
				int nextmgid = js.countnextmgid();
				medalgrade nextmg = mgmap.get(String.valueOf(nextmgid));
				if(nextmg != null) {
					medalgrade mg = mgmap.get(String.valueOf(js.countmgid()));
					Collection<Currency> cost = Currency.create(mg.getCosttype(), mg.getCostid(), mg.getCostnum());
					level = new CurrencyHandle().costCheck(getPlayerCode(), cost);
				}
			}
			
			//检查升阶
			if(!stage) {
				int nextmrid = js.countnextmrid();
				medalrefine nextmr = mrmap.get(String.valueOf(nextmrid));
				if(nextmr != null) {
					medalrefine mr = mrmap.get(String.valueOf(js.countmrid()));
					if(js.getLevel() >= mr.getCondition()) {
						Collection<Currency> cost = Currency.create(mr.getCosttype(), mr.getCostid(), mr.getCostnum());
						stage = new CurrencyHandle().costCheck(getPlayerCode(), cost);						
					}
				}
			}
		}

		new TipHandle().updateTip(getPlayerCode(), TipType.玉玺_升级, level); // 满级
		new TipHandle().updateTip(getPlayerCode(), TipType.玉玺_升阶, stage); // 满级
	}
	
	/**
	 * 获取红点类型
//	 * @param type
	 * @return
	 */
//	private TipType getleveltiptype(int type) {
//
//		if (type == 1)
//			return TipType.玉玺_升级_阵营1;
//		if (type == 2)
//			return TipType.玉玺_升级_阵营2;
//		if (type == 3)
//			return TipType.玉玺_升级_阵营3;
//		if (type == 4)
//			return TipType.玉玺_升级_阵营4;
//		if (type == 5)
//			return TipType.玉玺_升级_阵营5;
//		if (type == 6)
//			return TipType.玉玺_升级_阵营6;
//		return null;
//	}
	
//	private TipType getstagetiptype(int type) {
//		if (type == 1)
//			return TipType.玉玺_升阶_阵营1;
//		if (type == 2)
//			return TipType.玉玺_升阶_阵营2;
//		if (type == 3)
//			return TipType.玉玺_升阶_阵营3;
//		if (type == 4)
//			return TipType.玉玺_升阶_阵营4;
//		if (type == 5)
//			return TipType.玉玺_升阶_阵营5;
//		if (type == 6)
//			return TipType.玉玺_升阶_阵营6;
//		return null;
//	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				JadeSealModule jadeSealModule = getv(key(playerCode));
				if(jadeSealModule != null)MySQLCacheDAO.setV(key, jadeSealModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================
	@Override
	public String lockKey() {
		// TODO Auto-generated method stub
		return null;
	}

}
